package com.leo;

import javafx.application.Application;
import javafx.collections.ObservableList;
import javafx.collections.transformation.FilteredList;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
import javafx.util.StringConverter;

import java.util.function.Predicate;

/**
 * @ClassName ComboBoxDemo
 * @Description
 * @Author wangss
 * @date 2020.05.25 11:45
 * @Version 1.0
 */
public class ComboBox3Demo extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        AnchorPane anchorPane = new AnchorPane();
        anchorPane.setStyle("-fx-background-color: #e7eeea");
        Scene scene = new Scene(anchorPane);

        Button button = new Button("点击");

        Student s1 = new Student("小a", 12, 99d);
        Student s2 = new Student("小b", 18, 63d);
        Student s3 = new Student("小c", 20, 60d);
        Student s4 = new Student("小d", 20, 60d);
        Student s5 = new Student("小e", 20, 60d);

        ComboBox<Student> comboBox = new ComboBox<>();
        comboBox.getItems().addAll(s1, s2, s3, s4, s5);

        // 可以编辑
        comboBox.setEditable(true);
        // 文本提示
        comboBox.setPromptText("请输入内容");

        comboBox.setConverter(new StringConverter<Student>() {
            @Override
            public String toString(Student object) {
                if (object == null) {
                    return "";
                }

                return object.getName();
            }

            @Override
            public Student fromString(String string) {
                return null;
            }
        });

        AnchorPane.setTopAnchor(comboBox, 10d);
        AnchorPane.setLeftAnchor(comboBox, 10d);
        AnchorPane.setTopAnchor(button, 10d);
        AnchorPane.setLeftAnchor(button, 260d);
        anchorPane.getChildren().addAll(comboBox, button);

        primaryStage.setScene(scene);
        primaryStage.setTitle("combobox");
        primaryStage.setWidth(625d);
        primaryStage.setHeight(500d);
        primaryStage.setResizable(false);
        primaryStage.show();

        comboBox.getSelectionModel().selectedIndexProperty().addListener((observable, oldValue, newValue) -> {

        });

        comboBox.getSelectionModel().selectedItemProperty().addListener((observable, oldValue, newValue) -> {

        });

        ObservableList<Student> observableList = comboBox.getItems();
//        TextField editor = comboBox.getEditor();
        TextField editor = comboBox.editorProperty().get();
        editor.textProperty().addListener((observable, oldValue, newValue) -> {
            if (newValue == null || "".equals(newValue)) {
                comboBox.setItems(null);
                comboBox.setPlaceholder(new Label("没有找到"));
                return;
            }
            System.out.println(newValue);
            FilteredList<Student> filtered = observableList.filtered(new Predicate<Student>() {
                @Override
                public boolean test(Student student) {
                    return student.getName().contains(newValue);
                }
            });

            if (filtered == null) {
                comboBox.setItems(null);
                comboBox.setPlaceholder(new Label("没有找到"));
                return;
            }

            comboBox.hide();
            comboBox.setItems(filtered);
            comboBox.show();
        });
    }
}
